<?php
require_once("BaseDAO.php");
require_once("../Entities/History.php");

class HistoryDAO extends BaseDAO
{
	function CreateHistory($item)
	{
		$sql = "insert into Histories(UserId ,CreationOn, TotalCorrect, TotalQuestions, Marks, TotalMarks) values ('" . 
			$item->UserId . "',NOW(),'" . $item->TotalCorrect . "','" . $item->TotalQuestions . "','" . $item->Marks . "','" .
			$item->TotalMarks . "');";
		mysql_query($sql);
		$item->Id = mysql_insert_id();
		return $item;
	}
	
	function GetHistoris($page, $pageSize, &$totalPage, $userId, $dateFrom, $dateTo)
	{
//		$sqlTotalPage = "select * from Histories where UserId='" . $userId . "' and CreationOn >= '" . $dateFrom . "' and CreationOn <= '" . $dateTo . "';";
		$sqlTotalPage = "select * from Histories where UserId='" . $userId . "';";
		$sql = "select * from Histories where UserId='" . $userId ."' limit " . ($page - 1) * $pageSize . "," . $pageSize . ";";
//		$sql = "select * from Histories where UserId='" . $userId . "' and CreationOn >= STR_TO_DATE('" . $dateFrom . "', '%d/%m/%Y') and CreationOn <= STR_TO_DATE('" . $dateTo ."', '%d/%m/%Y') limit " . ($page - 1) * $pageSize . "," . $pageSize . ";";
		
		$resultTotalPage = mysql_query($sqlTotalPage);
		$result = mysql_query($sql);
		$totalPage = ceil(mysql_num_rows($resultTotalPage) / $pageSize);
		$list = array();
		while($row = mysql_fetch_assoc($result))
		{
			$item = new History();
			$item->Id = $row["Id"];
			$item->UserId = $row["UserId"];
			$item->CreationOn = $row["CreationOn"];
			$item->TotalCorrect = $row["TotalCorrect"];
			$item->TotalQuestions = $row["TotalQuestions"];
			$item->Marks = $row["Marks"];
			$item->TotalMarks = $row["TotalMarks"];
			$list[] = $item;
		}
		return $list;
	}
}

?>